﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using NetAccess.Client;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace SimpleChat.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class JoinServerViewModel : ViewModelBase
    {
        #region Properties
        public SafeObservableCollection<ServiceData> DiscoveredServices { get; private set; }

        #region Password
        /// <summary>
        /// The <see cref="Password" /> property's name.
        /// </summary>
        public const string PasswordPropertyName = "Password";

        private string _storePassword = null;

        /// <summary>
        /// Gets the Username property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string Password
        {
            get
            {
                return _storePassword;
            }

            set
            {
                if (_storePassword == value)
                    return;

                _storePassword = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(PasswordPropertyName);
            }
        }
        #endregion

        #region Username
        /// <summary>
        /// The <see cref="Username" /> property's name.
        /// </summary>
        public const string UsernamePropertyName = "Username";

        private string _storeUsername = null;

        /// <summary>
        /// Gets the Username property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string Username
        {
            get
            {
                return _storeUsername;
            }

            set
            {
                if (_storeUsername == value)
                    return;

                _storeUsername = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(UsernamePropertyName);
            }
        }
        #endregion

        #region SelectedService
        /// <summary>
        /// The <see cref="SelectedService" /> property's name.
        /// </summary>
        public const string SelectedServicePropertyName = "SelectedService";

        private ServiceData _storeSelectedService = null;

        /// <summary>
        /// Gets the SelectedService property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ServiceData SelectedService
        {
            get
            {
                return _storeSelectedService;
            }

            set
            {
                if (_storeSelectedService == value)
                    return;

                _storeSelectedService = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(SelectedServicePropertyName);
            }
        }
        #endregion

        #region WaitDiscover
        /// <summary>
        /// The <see cref="WaitDiscover" /> property's name.
        /// </summary>
        public const string WaitDiscoverPropertyName = "WaitDiscover";

        private bool _storeWaitDiscover = true;

        /// <summary>
        /// Gets the WaitDiscover property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public bool WaitDiscover
        {
            get
            {
                return _storeWaitDiscover;
            }

            set
            {
                if (_storeWaitDiscover == value)
                    return;
                
                _storeWaitDiscover = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(WaitDiscoverPropertyName);
            }
        }
        #endregion

        #region Commands
        public RelayCommand RefreshCommand {get; set;}
        #endregion
        #endregion
        
        public JoinServerViewModel()
            : base(Messenger.Default)
        {
            DiscoveredServices = new SafeObservableCollection<ServiceData>();

            if (IsInDesignMode)
            {
                Username = "Username";

                // Code runs in Blend --> create design time data.  
                DiscoveredServices.Add(new ServiceData
                {
                    ServiceName = "Test",
                    IP = System.Net.IPAddress.Parse("127.0.0.1"),
                    TCPPort = 3000,
                    NbClient = 12
                });
            }
            else
            {
                RefreshCommand = new RelayCommand(HandleRefreshCommand);
            }
        }

        private void HandleRefreshCommand()
        {
            DiscoveredServices.Clear();
            WaitDiscover = false;
            Network.Client.BeginDiscovery(
                service => DiscoveredServices.Add(service),
                () =>
                {
                    WaitDiscover = true;
                });            
        }
    }
}
